package com.xxzx.publicprevention.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.xxzx.publicprevention.pojo.GeoZhyfq;
import com.xxzx.publicprevention.pojo.GeoZhyhd;
import com.xxzx.publicprevention.pojo.TbUploadfile;
import com.xxzx.publicprevention.pojo.TbYfqfilerelation;
import com.xxzx.publicprevention.service.GeoZhyfqService;
import com.xxzx.publicprevention.service.GeoZhyhdService;
import com.xxzx.publicprevention.service.TbYfqfilerelationService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * (GeoZhyfq)表控制层
 *
 * @author makejava
 * @since 2020-11-12 12:56:42
 */
@RestController
@RequestMapping("geoZhyfq")
public class GeoZhyfqController {
    /**
     * 服务对象
     */
    @Resource
    private GeoZhyfqService geoZhyfqService;
    @Resource
    private TbYfqfilerelationService tbYfqfilerelationService;
    @Resource
    private TbUploadfileController tbUploadfileController;
    @Resource
    private GeoZhyhdService geoZhyhdService;


    @GetMapping("getInfoByid")
    public GeoZhyfq getInfoByid(Integer id) {
        try {
            return geoZhyfqService.queryById(id);
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("gis_Contains")
    public List<GeoZhyfq> gis_Contains(String coords) {
        try {
            return geoZhyfqService.gis_Contains(coords);
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("getZhyfqByKeywords")
    public List<GeoZhyfq> getZhyfqByKeywords(String keywords) {
        try {
            List<GeoZhyfq> geoZhyfqs = geoZhyfqService.getZhyfqByKeywords(keywords);
            return geoZhyfqs;
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("getZhyfqByNd")
    public List<GeoZhyfq> getZhyfqByNd(String nd) {
        try {
            List<GeoZhyfq> geoZhyfqs = geoZhyfqService.getZhyfqByNd(nd);
            return geoZhyfqs;
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("getZhyfqByNdAndKeywords")
    public List<GeoZhyfq> getZhyfqByNdAndKeywords(String nd, String keywords) {
        try {
            List<GeoZhyfq> geoZhyfqs = geoZhyfqService.getZhyfqByNdAndKeywords(nd, keywords);
            return geoZhyfqs;
        } catch (Exception ee) {
            return null;
        }
    }


    @GetMapping("getYfqYear")
    public List<String> getYfqYear() {
        try {

            return geoZhyfqService.getYfqYear();

        } catch (Exception ee) {
            return null;
        }
    }


    @PostMapping("GetAll")
    public List<GeoZhyfq> GetAll(@RequestBody GeoZhyfq geoZhyfq) {
        try {
            return geoZhyfqService.queryAll(geoZhyfq);
        } catch (Exception ee) {
            System.out.println(ee);
            return null;
        }
    }

    @PostMapping("update")
    @ResponseBody
    public int update(String yfq, String attaIds) {
        try {

            GeoZhyfq geoZhyfq = JSON.parseObject(yfq, GeoZhyfq.class);
            GeoZhyfq update = geoZhyfqService.update(geoZhyfq);

            List<TbYfqfilerelation> relationsByZhyfqid = tbYfqfilerelationService.getRelationsByZhyfqid(geoZhyfq.getId());
            List<Integer> aIds = JSONArray.parseArray(attaIds, Integer.class);

            if (relationsByZhyfqid.size() > 0) {
                for (TbYfqfilerelation c : relationsByZhyfqid) {


                    boolean flag = true;
                    if (aIds != null) {
                        for (Integer a : aIds) {
                            if (a.equals(c.getFileid())) {
                                flag = false;
                            }
                        }
                    }

                    if (flag) {
                        tbUploadfileController.deleteById(c.getFileid());
                    }


                    tbYfqfilerelationService.deleteById(c.getId());


                }
            }


            addFile(attaIds, geoZhyfq.getId());
            return update.getId();
        } catch (Exception ee) {
            ee.getMessage();
            return -1;
        }
    }

    private void addFile(String attaIds, Integer id) {
        // 关联附件
        List<Integer> aIds = JSONArray.parseArray(attaIds, Integer.class);
        if (aIds != null) {
            TbYfqfilerelation rela;
            for (Integer aid : aIds) {
                rela = new TbYfqfilerelation();
                rela.setYfqid(id);
                rela.setFileid(aid);
                tbYfqfilerelationService.insert(rela);
            }
        }
    }


    @PostMapping("insert")
    @ResponseBody
    public int insert(String yfq, String attaIds) {
        try {

            GeoZhyfq geoZhyfq = JSON.parseObject(yfq, GeoZhyfq.class);

            GeoZhyfq insert = geoZhyfqService.insert(geoZhyfq);
            // 关联附件
            addFile(attaIds, insert.getId());
            return insert.getId();
        } catch (Exception ee) {
            ee.getMessage();
            return -1;
        }
    }


    @GetMapping("delete")
    public boolean delete(Integer id) {
        try {
            deleteFile(id);

            return geoZhyfqService.deleteById(id);
        } catch (Exception ee) {
            return false;
        }
    }


    private void deleteFile(Integer id) {
        List<TbYfqfilerelation> relationsByZhyfqid = tbYfqfilerelationService.getRelationsByZhyfqid(id);
        if (relationsByZhyfqid.size() > 0) {
            for (TbYfqfilerelation c : relationsByZhyfqid) {
                tbUploadfileController.deleteById(c.getFileid());
                tbYfqfilerelationService.deleteById(c.getId());
            }
        }
    }


    @RequestMapping(value = "/getFileByZhyfqid", method = RequestMethod.GET)
    @ResponseBody
    public List<TbUploadfile> getFileByZhyfqid(int id) {
        List<TbUploadfile> list = new ArrayList<>();
        try {
            List<TbYfqfilerelation> relations = tbYfqfilerelationService.getRelationsByZhyfqid(id);
            if (relations.size() > 0) {
                for (TbYfqfilerelation c : relations) {
                    TbUploadfile file = tbUploadfileController.selectOne(c.getFileid());
                    list.add(file);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;

    }


}
